#pragma once

#include <QString>
#include <optional>
#include "../nlohmann/json.hpp"

using json = nlohmann::json;

class User {
public:
	std::optional<int> userId;
	std::optional<QString> username;
	std::optional<QString> password;
	std::optional<QString> email;
	std::optional<QString> phone;
	std::optional<QString> role;
	std::optional<QString> createdAt;
	std::optional<QString> updatedAt;

	json toJson() const {
		json j;
		j["userId"] = userId.has_value() ? json(*userId) : json(nullptr);
		j["username"] = username.has_value() ? json(username->toStdString()) : json(nullptr);
		j["password"] = password.has_value() ? json(password->toStdString()) : json(nullptr);
		j["email"] = email.has_value() ? json(email->toStdString()) : json(nullptr);
		j["phone"] = phone.has_value() ? json(phone->toStdString()) : json(nullptr);
		j["role"] = role.has_value() ? json(role->toStdString()) : json(nullptr);
		j["createdAt"] = createdAt.has_value() ? json(createdAt->toStdString()) : json(nullptr);
		j["updatedAt"] = updatedAt.has_value() ? json(updatedAt->toStdString()) : json(nullptr);
		return j;
	}

	static User fromJson(const json &j) {
		User u;
		if (j.contains("userId") && !j["userId"].is_null()) u.userId = j["userId"].get<int>();
		if (j.contains("username") && !j["username"].is_null())
			u.username = QString::fromStdString(
				j["username"].get<std::string>());
		if (j.contains("password") && !j["password"].is_null())
			u.password = QString::fromStdString(
				j["password"].get<std::string>());
		if (j.contains("email") && !j["email"].is_null())
			u.email = QString::fromStdString(
				j["email"].get<std::string>());
		if (j.contains("phone") && !j["phone"].is_null())
			u.phone = QString::fromStdString(
				j["phone"].get<std::string>());
		if (j.contains("role") && !j["role"].is_null()) u.role = QString::fromStdString(j["role"].get<std::string>());
		if (j.contains("createdAt") && !j["createdAt"].is_null())
			u.createdAt = QString::fromStdString(
				j["createdAt"].get<std::string>());
		if (j.contains("updatedAt") && !j["updatedAt"].is_null())
			u.updatedAt = QString::fromStdString(
				j["updatedAt"].get<std::string>());
		return u;
	}
};
